package cn.afactory.tygz.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.user.UserInfoUpdateActivity;


public class MyTextActivity extends BaseActivity {
	public final static int MAX_LENGTH_NAME = 12;
	public final static int MAX_LENGTH_SIGN = 40;
	public final static int MAX_LENGTH_INTRO = 100;
	public final static int MAX_LENGTH_DEST = 100;

	private final static int MAX_NICK_LENGTH  = 20;
	private final static int MIN_NICK_LENGTH  = 2;

	private final static int MAX_COMPANY_LENGTH  = 20;
	private final static int MIN_COMPANY_LENGTH  = 2;
	private int mSelectType;
	private String mTitle;
	private String mText;

	private EditText post_comment_edittext;
	private TextView tv_hint_post_comment;
	private ImageView mClearImg;
	private TextView mTvErrorHint;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.ac_my_text);
		showBackView();

		mSelectType = getIntent().getIntExtra(SPUtils.EXTRA_SELECT_TYPE, -1);
		mTitle = getIntent().getStringExtra(SPUtils.EXTRA_TITLE);
		mText = getIntent().getStringExtra(SPUtils.EXTRA_SELECT_CURRENT_VALUE);
		if (mTitle != null) {
			setTitleText(mTitle);
		}

		setRightButton("提交", new OnClickListener() {
			@Override
			public void onClick(View v) {
				doPostComment();
			}
		});

		loadData();
	}

	/**
	 * 选择文本
	 * @param context
	 */
	public static void gotoSeclectText(Activity context,String title,int type,String value){
		Intent intent = new Intent(context,MyTextActivity.class);
		intent.putExtra(SPUtils.EXTRA_TITLE,title);
		intent.putExtra(SPUtils.EXTRA_SELECT_TYPE,type);
		intent.putExtra(SPUtils.EXTRA_SELECT_CURRENT_VALUE,value);
		context.startActivityForResult(intent, UserInfoUpdateActivity.REQ_TEXT_INPUT);
	}

	private void loadData() {
		post_comment_edittext = (EditText) findViewById(R.id.post_comment_edittext);
		if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_NICK_NAME) {
			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) post_comment_edittext
					.getLayoutParams();
			lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;

			post_comment_edittext.setLayoutParams(lp);
			post_comment_edittext.setSingleLine();
		}
		post_comment_edittext.setText(mText);
		// post_comment_edittext.clearComposingText();
		tv_hint_post_comment = (TextView) findViewById(R.id.tv_hint_post_comment);

		mClearImg = (ImageView) findViewById(R.id.img_clear);
		mClearImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				post_comment_edittext.setText("");
			}
		});

		mTvErrorHint = (TextView) findViewById(R.id.tv_error_hint);

		limitText();
	}

	private void doPostComment() {
		String suggestText = post_comment_edittext.getText().toString();

		if (mContentIsLeagal && !TextUtils.isEmpty(suggestText)
				&& !suggestText.equals(mText)) {
			Intent data = new Intent();
			data.putExtra(SPUtils.EXTRA_SELECT_CURRENT_VALUE, suggestText);
			data.putExtra(SPUtils.EXTRA_SELECT_TYPE,mSelectType);
			setResult(Activity.RESULT_OK, data);
			finish();
		}
	}

	private void limitText() {

		if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_NICK_NAME) {
			limitsize = MAX_LENGTH_NAME;
			post_comment_edittext.setSingleLine();
		} else if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_DEST) {
			limitsize = MAX_LENGTH_DEST;
		} else if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_INTRO) {
			limitsize = MAX_LENGTH_INTRO;
		} else if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_SIGN) {
			limitsize = MAX_LENGTH_SIGN;
		} else if (mSelectType == UserInfoUpdateActivity.SELECT_TYPE_COMPANY) {
			limitsize = MAX_COMPANY_LENGTH;
		}
		post_comment_edittext.addTextChangedListener(mTextWatcher);
		post_comment_edittext.setSelection(post_comment_edittext.length()); // 将光标移动最后一个字符后面
		checkEditContent();
	}

	private int limitsize = MAX_LENGTH_NAME;

	private TextWatcher mTextWatcher = new TextWatcher() {

		private int editStart;

		private int editEnd;

		public void afterTextChanged(Editable editable) {
			checkEditContent();
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};

	/**
	 * 计算分享内容的字数，一个汉字=两个英文字母，一个中文标点=两个英文标点 注意：该函数的不适用于对单个字符进行计算，因为单个字符四舍五入后都是1
	 * 
	 * @param c
	 * @return 返回字节个数
	 */
	private int calculateLength(CharSequence c) {
		int len = 0;
		for (int i = 0; i < c.length(); i++) {
			int tmp = (int) c.charAt(i);
			if (tmp > 0 && tmp < 127) {
				len += 1;
			} else {
				len += 2;
			}
		}
		return len;
	}

	private boolean mContentIsLeagal = true;

	/**
	 * 刷新剩余输入字数,最大值新浪微博是140个字，人人网是200个字
	 */
	private void checkEditContent() {
		mContentIsLeagal = true;
//		int inputLen = getInputCount();
		int currentLength = post_comment_edittext.getText().toString().length();
		if (currentLength == 0) {
			mClearImg.setVisibility(View.GONE);
			mContentIsLeagal = false;
		} else {
			mClearImg.setVisibility(View.VISIBLE);
		}
		
		tv_hint_post_comment.setText(String.format(getString(R.string.feed_back_left_char),
				String.valueOf(MAX_NICK_LENGTH - currentLength)));

		
		if(post_comment_edittext.getText().toString().equals(mText)){
			mContentIsLeagal = false;
		}

		if(mSelectType == UserInfoUpdateActivity.SELECT_TYPE_NICK_NAME){
			if (currentLength < MIN_NICK_LENGTH) {
				mTvErrorHint.setText(String.format(getString(R.string.error_nick_must_bigger_than_two),mTitle,MAX_NICK_LENGTH));
				mContentIsLeagal = false;
			} else if (MAX_NICK_LENGTH - currentLength < 0) {
				mTvErrorHint.setText(String.format(getString(R.string.error_must_litter_than_tweety),MAX_LENGTH_DEST,mTitle));
				mContentIsLeagal = false;
			} else {
				String content = post_comment_edittext.getText().toString();

				String REG = "^[a-zA-Z0-9\u4e00-\u9fa5]+$";
				if (!content.matches(REG)) { // 不符合规则
					mTvErrorHint.setText(getString(R.string.error_must_not_contain_spec_char));
					mContentIsLeagal = false;
				}
			}
		} else if(mSelectType == UserInfoUpdateActivity.SELECT_TYPE_DEST){
			if (currentLength < MIN_NICK_LENGTH) {
				mTvErrorHint.setText(String.format(getString(R.string.error_nick_must_bigger_than_two),mTitle,MAX_LENGTH_DEST));
				mContentIsLeagal = false;
			} else if (MAX_LENGTH_DEST - currentLength < 0) {
				mTvErrorHint.setText(String.format(getString(R.string.error_must_litter_than_tweety),MAX_LENGTH_DEST,mTitle));
				mContentIsLeagal = false;
			} /*else {
				String content = post_comment_edittext.getText().toString();

				String REG = "^[a-zA-Z0-9\u4e00-\u9fa5]+$";
				if (!content.matches(REG)) { // 不符合规则
					mTvErrorHint.setText(getString(R.string.error_must_not_contain_spec_char));
					mContentIsLeagal = false;
				}
			}*/
		} else if(mSelectType == UserInfoUpdateActivity.SELECT_TYPE_SIGN){
			if (currentLength < MIN_NICK_LENGTH) {
				mTvErrorHint.setText(String.format(getString(R.string.error_nick_must_bigger_than_two),mTitle,MAX_LENGTH_SIGN));
				mContentIsLeagal = false;
			} else if (MAX_LENGTH_SIGN - currentLength < 0) {
				mTvErrorHint.setText(String.format(getString(R.string.error_must_litter_than_tweety),MAX_LENGTH_SIGN,mTitle));
				mContentIsLeagal = false;
			} /*else {
				String content = post_comment_edittext.getText().toString();

				String REG = "^[a-zA-Z0-9\u4e00-\u9fa5]+$";
				if (!content.matches(REG)) { // 不符合规则
					mTvErrorHint.setText(getString(R.string.error_must_not_contain_spec_char));
					mContentIsLeagal = false;
				}
			}*/
		} else if(mSelectType == UserInfoUpdateActivity.SELECT_TYPE_INTRO){
			if (currentLength < MIN_NICK_LENGTH) {
				mTvErrorHint.setText(String.format(getString(R.string.error_nick_must_bigger_than_two),mTitle,MAX_LENGTH_INTRO));
				mContentIsLeagal = false;
			} else if (MAX_LENGTH_INTRO - currentLength < 0) {
				mTvErrorHint.setText(String.format(getString(R.string.error_must_litter_than_tweety),MAX_LENGTH_INTRO,mTitle));
				mContentIsLeagal = false;
			} /*else {
				String content = post_comment_edittext.getText().toString();

				String REG = "^[a-zA-Z0-9\u4e00-\u9fa5]+$";
				if (!content.matches(REG)) { // 不符合规则
					mTvErrorHint.setText(getString(R.string.error_must_not_contain_spec_char));
					mContentIsLeagal = false;
				}
			}*/
		} else if(mSelectType == UserInfoUpdateActivity.SELECT_TYPE_COMPANY){
			if (currentLength < MIN_COMPANY_LENGTH) {
				mTvErrorHint.setText(String.format(getString(R.string.error_nick_must_bigger_than_two),mTitle,MAX_COMPANY_LENGTH));
				mContentIsLeagal = false;
			} else if (MAX_COMPANY_LENGTH - currentLength < 0) {
				mTvErrorHint.setText(String.format(getString(R.string.error_must_litter_than_tweety),MAX_COMPANY_LENGTH,mTitle));
				mContentIsLeagal = false;
			}
		}


		if (mContentIsLeagal && mTvErrorHint != null) {
			mTvErrorHint.setText("");
		}
	}

	/**
	 * 获取用户输入的分享内容字数
	 * 
	 * @return
	 */
	private int getInputCount() {
		return calculateLength(post_comment_edittext.getText().toString());
	}

}